package cn.com.cetccst.activation.ui;

import static android.app.Activity.RESULT_OK;
import static cn.com.cetccst.activation.ui.ScanQRActivity.INTENT_CAPTURE_CONTENT;
import static cn.com.cetccst.tools.kv.CommonKV.kvStore;

import android.Manifest;
import android.content.Intent;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.google.gson.Gson;

import cn.com.cetccst.activation.BR;
import cn.com.cetccst.activation.R;
import cn.com.cetccst.activation.databinding.FragmentActivationBinding;
import cn.com.cetccst.activation.kv.KVKeys;
import cn.com.cetccst.activation.ui.dialog.ActivationProcessDialog;
import cn.com.cetccst.activation.ui.dialog.viewmodel.ProcessViewModel;
import cn.com.cetccst.activation.ui.viewmodel.ActivationViewModel;
import cn.com.cetccst.tools.pm.AppPackage;
import cn.com.cetccst.uicomponents.binding.app.DataBindingConfig;
import cn.com.cetccst.uicomponents.binding.app.DataBindingFragment;

public class ActivationFragment extends DataBindingFragment<FragmentActivationBinding> {

    private ActivationViewModel viewModel;
    private ProcessViewModel processViewModel;
    private ActivationProcessDialog activationDialog;
    private final ActivityResultLauncher<Intent> barcodeLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK) {
                    Intent data = result.getData();
                    if (data != null) {
                        try {
                            //进行数据操作
                            viewModel.authData.setValue(new Gson().fromJson(data.getStringExtra(INTENT_CAPTURE_CONTENT), ActivationViewModel.AuthData.class));
                            kvStore.encode(KVKeys.KEY_BASE_URL, viewModel.authData.getValue().getOtaServer());
                        } catch (Exception e) {
                            Toast.makeText(getActivity(), "二维码信息无效", Toast.LENGTH_SHORT).show();
                        }

                    } else {
                        Toast.makeText(getActivity(), R.string.scan_cancelled, Toast.LENGTH_SHORT).show();
                    }
                }
            });

    @Override
    protected void initViewModel() {
        viewModel = getFragmentScopeViewModel(ActivationViewModel.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        init();

        return new DataBindingConfig(R.layout.fragment_activation, BR.viewModel, viewModel)
                .addBindingParam(BR.click, new Click());
    }

    private void init() {
        processViewModel = new ProcessViewModel();
        activationDialog = new ActivationProcessDialog(getActivity(), processViewModel);
        activationDialog.setOwnerActivity(getActivity());
        activationDialog.setCancelable(false);

        viewModel.setProcessViewModel(processViewModel);

        viewModel.registerObserver(getActivity(), new ProcessViewModel.Callback() {
            @Override
            public void onStart() {
                if (!activationDialog.isShowing())
                    activationDialog.show();
            }

            @Override
            public void onSuccess() {
                if (activationDialog.isShowing())
                    activationDialog.dismiss();
                gotoNext();
            }
        });
    }

    private void gotoNext() {
        NavController navController = Navigation.findNavController(getBinding().getRoot());
        navController.navigate(R.id.action_activationFragment_to_registerFragment, null);
    }

    public class Click {
        public void scan() {
            AppPackage.requestPermission(getActivity(),
                    getString(R.string.camera_permission_des), (allGranted, grantedList, deniedList) -> {
                        if (allGranted) {
                            barcodeLauncher.launch(new Intent(getActivity(), ScanQRActivity.class));
                        }
                    },
                    Manifest.permission.CAMERA);
        }
    }
}